package cn.well.cloud.base.wechat.api.impl;

import cn.well.cloud.base.util.HttpUtils;
import cn.well.cloud.base.wechat.api.TemplateMessageAPI;
import cn.well.cloud.base.wechat.model.NoticeInfo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class TemplateMessage  implements TemplateMessageAPI {

    @Override
    public Object sendTempMessage(String access_token, NoticeInfo noticeInfo) throws IOException {
        String doUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token;

        Object touser = noticeInfo.getTouser();//接收者openid
        String templateId = noticeInfo.getTemplate_id();//模板ID
        Object url = noticeInfo.getUrl();//模板跳转链接，如果置空，则在发送后，点击模板消息会进入一个空白页面（ios），或无法点击（android）。

        Object first = noticeInfo.getFirst();//标题
        Object remark = noticeInfo.getRemark();//备注
        Object keyword1 = noticeInfo.getKeyword1();
        Object keyword2 = noticeInfo.getKeyword2();
        Object keyword3 = noticeInfo.getKeyword3();

        JSONObject parentJSON = new JSONObject();
        parentJSON.put("touser", touser);
        parentJSON.put("template_id", templateId);
        parentJSON.put("url", url);

        JSONObject json = new JSONObject();
        json.put("first", first);
        json.put("keyword1", keyword1);//对应的车辆信息
        json.put("keyword2", keyword2);//产品信息
        json.put("keyword3", keyword3);//出单状态
        json.put("remark",  remark);
        parentJSON.put("data", json);//模板数据
        String result = HttpUtils.doPost(doUrl,parentJSON.toJSONString(),"plain");

        JSONObject parseObject = JSONArray.parseObject(result);
        int errcode = parseObject.getIntValue("errcode");
        log.info("返回信息："+parseObject.toJSONString());
        if(errcode ==0){
            log.info("发送成功!");
        }
        return parseObject;
    }
}
